{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 作业三"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 读取数据\n",
    "from sklearn.datasets import fetch_openml    \n",
    "mnist = fetch_openml('mnist_784')\n",
    "X, y = mnist['data'], mnist['target']\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(70000, 784)"
      ]
     },
     "execution_count": 21,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "X.shape"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 打乱顺序\n",
    "import numpy as np\n",
    "shuffle_index=np.random.permutation(70000)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "metadata": {},
   "outputs": [],
   "source": [
    "X = X[shuffle_index]\n",
    "y=y[shuffle_index]\n",
    "X_train,y_train,X_test,y_test = X[:60000],y[:60000],X[60000:],y[60000:]\n",
    "import pandas as pd \n",
    "import matplotlib.pyplot as plt"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[  0.,   0.,   0.,   0.,   0.,   0.,   0.,   0.,   0.,   0.,   0.,\n",
       "          0.,   0.,   0.,   0.,   0.,   0.,   0.,   0.,   0.,   0.,   0.,\n",
       "          0.,   0.,   0.,   0.,   0.,   0.],\n",
       "       [  0.,   0.,   0.,   0.,   0.,   0.,   0.,   0.,   0.,   0.,   0.,\n",
       "          0.,   0.,   0.,   0.,   0.,   0.,   0.,   0.,   0.,   0.,   0.,\n",
       "          0.,   0.,   0.,   0.,   0.,   0.],\n",
       "       [  0.,   0.,   0.,   0.,   0.,   0.,   0.,   0.,   0.,   0.,   0.,\n",
       "          0.,   0.,   0.,   0.,   0.,   0.,   0.,   0.,   0.,   0.,   0.,\n",
       "          0.,   0.,   0.,   0.,   0.,   0.],\n",
       "       [  0.,   0.,   0.,   0.,   0.,   0.,   0.,   0.,   0.,   0.,   0.,\n",
       "          0.,   0.,   0.,   0.,   0.,   0.,   0.,   0.,   0.,   0.,   0.,\n",
       "          0.,   0.,   0.,   0.,   0.,   0.],\n",
       "       [  0.,   0.,   0.,   0.,   0.,   0.,   0.,   0.,   0.,   0.,   0.,\n",
       "          0.,   0.,   0.,   0.,   0.,   0.,   0.,   0.,   0.,   0.,   0.,\n",
       "          0.,   0.,   0.,   0.,   0.,   0.],\n",
       "       [  0.,   0.,   0.,   0.,   0.,   0.,   0.,   0.,   0.,   0.,   0.,\n",
       "          0.,   0.,   0.,   0.,   0.,   0.,   0.,   0.,   0.,   0.,   0.,\n",
       "          0.,   0.,   0.,   0.,   0.,   0.],\n",
       "       [  0.,   0.,   0.,   0.,   0.,   0.,   0., 107., 232., 248., 248.,\n",
       "        248., 214., 118.,  33.,   0.,   0.,   0.,   0.,   0.,   0.,   0.,\n",
       "          0.,   0.,   0.,   0.,   0.,   0.],\n",
       "       [  0.,   0.,   0.,  55., 112., 235., 241., 252., 253., 253., 253.,\n",
       "        253., 253., 253., 128.,   0.,   0.,   0.,   0.,   0.,   0.,   0.,\n",
       "          0.,   0.,   0.,   0.,   0.,   0.],\n",
       "       [  0.,   0.,   0., 178., 253., 253., 253., 253., 162., 149., 149.,\n",
       "        149., 229., 253., 201.,   0.,   0.,   0.,   0.,   0.,   0.,   0.,\n",
       "          0.,   0.,   0.,   0.,   0.,   0.],\n",
       "       [  0.,   0.,   0., 255., 253., 210., 102.,  25.,   4.,   0.,   0.,\n",
       "          0., 196., 253., 201.,   0.,   0.,   0.,   0.,   0.,   0.,   0.,\n",
       "          0.,   0.,   0.,   0.,   0.,   0.],\n",
       "       [  0.,   0.,   0.,  96., 102.,  18.,   0.,   0.,   0.,   0.,   0.,\n",
       "        177., 246., 253.,  87.,   0.,   0.,   0.,   0.,   0.,   0.,   0.,\n",
       "          0.,   0.,   0.,   0.,   0.,   0.],\n",
       "       [  0.,   0.,   0.,   0.,   0.,   0.,   0.,   0.,   0.,  29., 192.,\n",
       "        246., 253., 253., 226., 215., 195.,  75.,   0.,   0.,   0.,   0.,\n",
       "          0.,   0.,   0.,   0.,   0.,   0.],\n",
       "       [  0.,   0.,   0.,   0.,   0.,   0.,   0.,  31., 193., 224., 253.,\n",
       "        253., 253., 253., 253., 253., 253., 247., 195.,  73.,   0.,   0.,\n",
       "          0.,   0.,   0.,   0.,   0.,   0.],\n",
       "       [  0.,   0.,   0.,   0.,   0.,   0.,  30., 209., 253., 253., 253.,\n",
       "        233., 181., 181., 181., 194., 253., 253., 253., 249., 125.,   0.,\n",
       "          0.,   0.,   0.,   0.,   0.,   0.],\n",
       "       [  0.,   0.,   0.,   0.,   0.,   0.,  24., 175., 222., 188.,  81.,\n",
       "         42.,   0.,   0.,   0.,  11.,  58., 141., 228., 253., 250., 124.,\n",
       "          0.,   0.,   0.,   0.,   0.,   0.],\n",
       "       [  0.,   0.,   0.,   0.,   0.,   0.,   0.,   0.,  34.,   0.,   0.,\n",
       "          0.,   0.,   0.,   0.,   0.,   0.,   0.,  60., 253., 253., 247.,\n",
       "          0.,   0.,   0.,   0.,   0.,   0.],\n",
       "       [  0.,   0.,   0.,   0.,   0.,   0.,   0.,   0.,   0.,   0.,   0.,\n",
       "          0.,   0.,   0.,   0.,   0.,   0.,   0.,   8., 152., 253., 251.,\n",
       "         89.,   0.,   0.,   0.,   0.,   0.],\n",
       "       [  0.,   0.,   0.,   0.,   0.,   0.,   0.,   0.,   0.,   0.,   0.,\n",
       "          0.,   0.,   0.,   0.,   0.,   0.,   0.,   0.,  20., 253., 253.,\n",
       "        213.,   0.,   0.,   0.,   0.,   0.],\n",
       "       [  0.,   0.,   0.,   0.,   0.,   0.,   0.,   0.,   0.,   0.,   0.,\n",
       "          0.,   0.,   0.,   0.,   0.,   0.,   0.,   0.,  20., 253., 253.,\n",
       "        233.,   0.,   0.,   0.,   0.,   0.],\n",
       "       [  0.,   0.,   0.,   0.,   0.,   0.,   0.,   0.,   0.,   0.,   0.,\n",
       "          0.,   0.,   0.,   0.,   0.,   0.,   0.,  34., 170., 253., 252.,\n",
       "        108.,   0.,   0.,   0.,   0.,   0.],\n",
       "       [  0.,   0.,   0.,   0.,   0.,   0.,   0.,   0.,   0.,   0.,   9.,\n",
       "        130., 124.,  27.,  27.,  27.,  47., 156., 216., 253., 253., 247.,\n",
       "          0.,   0.,   0.,   0.,   0.,   0.],\n",
       "       [  0.,   0.,   0.,   0.,   0.,   0.,   0.,   0.,   0.,   0.,  32.,\n",
       "        223., 253., 253., 253., 253., 253., 253., 253., 253., 235., 101.,\n",
       "          0.,   0.,   0.,   0.,   0.,   0.],\n",
       "       [  0.,   0.,   0.,   0.,   0.,   0.,   0.,   0.,   0.,   0.,   0.,\n",
       "         88., 210., 250., 253., 253., 253., 253., 245., 230.,  48.,   0.,\n",
       "          0.,   0.,   0.,   0.,   0.,   0.],\n",
       "       [  0.,   0.,   0.,   0.,   0.,   0.,   0.,   0.,   0.,   0.,   0.,\n",
       "          0.,   0.,  87., 117., 117., 117., 117.,  45.,   0.,   0.,   0.,\n",
       "          0.,   0.,   0.,   0.,   0.,   0.],\n",
       "       [  0.,   0.,   0.,   0.,   0.,   0.,   0.,   0.,   0.,   0.,   0.,\n",
       "          0.,   0.,   0.,   0.,   0.,   0.,   0.,   0.,   0.,   0.,   0.,\n",
       "          0.,   0.,   0.,   0.,   0.,   0.],\n",
       "       [  0.,   0.,   0.,   0.,   0.,   0.,   0.,   0.,   0.,   0.,   0.,\n",
       "          0.,   0.,   0.,   0.,   0.,   0.,   0.,   0.,   0.,   0.,   0.,\n",
       "          0.,   0.,   0.,   0.,   0.,   0.],\n",
       "       [  0.,   0.,   0.,   0.,   0.,   0.,   0.,   0.,   0.,   0.,   0.,\n",
       "          0.,   0.,   0.,   0.,   0.,   0.,   0.,   0.,   0.,   0.,   0.,\n",
       "          0.,   0.,   0.,   0.,   0.,   0.],\n",
       "       [  0.,   0.,   0.,   0.,   0.,   0.,   0.,   0.,   0.,   0.,   0.,\n",
       "          0.,   0.,   0.,   0.,   0.,   0.,   0.,   0.,   0.,   0.,   0.,\n",
       "          0.,   0.,   0.,   0.,   0.,   0.]])"
      ]
     },
     "execution_count": 31,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 读取照片\n",
    "some_data = X_train[20000]\n",
    "some_data_img = some_data.reshape(28,28)\n",
    "some_data_img"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 32,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "iVBORw0KGgoAAAANSUhEUgAAAPsAAAD4CAYAAAAq5pAIAAAABHNCSVQICAgIfAhkiAAAAAlwSFlzAAALEgAACxIB0t1+/AAAADh0RVh0U29mdHdhcmUAbWF0cGxvdGxpYiB2ZXJzaW9uMy4yLjEsIGh0dHA6Ly9tYXRwbG90bGliLm9yZy+j8jraAAANf0lEQVR4nO3db4xVdX7H8c+ndFHCkgglIrog64bEVqNuJVgDabbZ7MZiDGzMKjyo1m6cfYBmTZq0iCFLMtaQtmt9oNkEsgoayob4p+JaumsI0fbJRjRU+SOrJXQXmAzSSVzWxFDk2wdzaEadc85w/507832/ksm993zvOeebGz6cc+/v3PtzRAjA1Pd7TTcAoDcIO5AEYQeSIOxAEoQdSOL3e7kz23z0D3RZRHi85W0d2W3fZvuI7Q9sr2tnWwC6y62Os9ueJulXkr4l6bikNyWtiYhDFetwZAe6rBtH9qWSPoiIoxFxVtJPJa1sY3sAuqidsF8l6TdjHh8vln2G7QHb+2zva2NfANrUzgd0450qfOE0PSI2S9oscRoPNKmdI/txSQvGPP6KpJPttQOgW9oJ+5uSFtv+qu3pklZL2tWZtgB0Wsun8RFxzvYDkn4uaZqkpyPiYMc6A9BRLQ+9tbQz3rMDXdeVi2oATB6EHUiCsANJEHYgCcIOJEHYgSQIO5AEYQeSIOxAEoQdSIKwA0kQdiAJwg4kQdiB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uit1b1pLdG37MD6J2mj+wAeoSwA0k0Enbbt9k+YvsD2+ua6KGM7WO23y2moW50frpiDr1Ttg+MWTbH9mu23y9ux51jr6He+mIa74ppxht97Zqe/rzn79ltT5P0K0nfknRc0puS1kTEoZ42UsL2MUlLIqLxCzBs/6mk30l69sLUWrb/XtJIRGwq/qOcHRF/2ye9bdRFTuPdpd7Kphn/SzX42nVy+vNWNHFkXyrpg4g4GhFnJf1U0soG+uh7EfGGpJHPLV4paVtxf5tG/7H0XElvfSEihiLi7eL+GUkXphlv9LWr6Ksnmgj7VZJ+M+bxcfXXfO8h6Re237I90HQz45h3YZqt4vbyhvv5vNppvHvpc9OM981r18r05+1qIuzjTU3TT+N/yyLijyX9uaS1xekqJubHkr6m0TkAhyT9qMlmimnGX5D0UET8tslexhqnr568bk2E/bikBWMef0XSyQb6GFdEnCxuT0l6SaNvO/rJ8IUZdIvbUw338/8iYjgiPo2I85K2qMHXrphm/AVJ2yPixWJx46/deH316nVrIuxvSlps+6u2p0taLWlXA318ge2ZxQcnsj1T0rfVf1NR75J0b3H/XkkvN9jLZ/TLNN5l04yr4deu8enPI6Lnf5JWaPQT+f+S9EgTPZT0dY2k/yz+Djbdm6QdGj2t+1+NnhF9T9IfSNoj6f3idk4f9facRqf2fkejwZrfUG/LNfrW8B1J+4u/FU2/dhV99eR143JZIAmuoAOSIOxAEoQdSIKwA0kQdiAJwg4kQdiBJP4P/SNfL1/Ac4AAAAAASUVORK5CYII=\n",
      "text/plain": [
       "<Figure size 432x288 with 1 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    }
   ],
   "source": [
    "%matplotlib inline\n",
    "plt.imshow(some_data_img,cmap = 'gray')\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 33,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 图片位移\n",
    "def image_offset(data_s,dir='u',offset=1):\n",
    "    data_r=data_s.reshape((len(data_s),int(np.sqrt(data_s.shape[1])),-1))\n",
    "    data_new=[]\n",
    "    if dir=='u':\n",
    "        for img_data in data_r:\n",
    "            img_data_new=np.concatenate((img_data[offset:,:],img_data[:offset,:]),axis=0)\n",
    "            data_new.append(img_data_new)\n",
    "        \n",
    "    elif dir=='d':\n",
    "        for img_data in data_r:\n",
    "            img_data_new=np.concatenate((img_data[(len(img_data)-offset):,:],img_data[:(len(img_data)-offset),:]),axis=0)\n",
    "            data_new.append(img_data_new)\n",
    "            \n",
    "    elif dir=='l':\n",
    "        for img_data in data_r:\n",
    "            img_data_new=np.concatenate((img_data[:,offset:],img_data[:,:offset]),axis=1)\n",
    "            data_new.append(img_data_new)\n",
    "            \n",
    "    elif dir=='r':\n",
    "        for img_data in data_r:\n",
    "            img_data_new=np.concatenate((img_data[:,(len(img_data)-offset):],img_data[:,:(len(img_data)-offset)]),axis=1)\n",
    "            data_new.append(img_data_new)\n",
    "    data_new=np.array(data_new)        \n",
    "    X_train=data_new.reshape(len(data_s),-1)\n",
    "    return X_train"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 34,
   "metadata": {},
   "outputs": [],
   "source": [
    "X_train_u=image_offset(X_train,dir='u')\n",
    "X_train_d=image_offset(X_train,dir='d')\n",
    "X_train_l=image_offset(X_train,dir='l')\n",
    "X_train_r=image_offset(X_train,dir='r')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 35,
   "metadata": {},
   "outputs": [],
   "source": [
    "X_train_new=np.concatenate((X_train,X_train_u,X_train_d,X_train_l,X_train_r),axis=0)\n",
    "y_train_new=np.concatenate((y_train,y_train,y_train,y_train,y_train),axis=0)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 36,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 实例化 \n",
    "from sklearn.neighbors import KNeighborsClassifier\n",
    "kn_clf=KNeighborsClassifier()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 41,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([False, False, False, ...,  True, False, False])"
      ]
     },
     "execution_count": 41,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "y_train_5=(y_train.astype(int)==5)\n",
    "y_train_5"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 42,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "KNeighborsClassifier(algorithm='auto', leaf_size=30, metric='minkowski',\n",
       "                     metric_params=None, n_jobs=None, n_neighbors=5, p=2,\n",
       "                     weights='uniform')"
      ]
     },
     "execution_count": 42,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "kn_clf.fit(X_train,y_train_5)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 43,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([False])"
      ]
     },
     "execution_count": 43,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 验证\n",
    "kn_clf.predict([some_data])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from sklearn.model_selection import cross_val_predict \n",
    "y_train_knn_pred = cross_val_predict(kn_clf, X_train, y_train_5, cv=3)\n",
    "from sklearn.metrics import precision_score,recall_score\n",
    "precision = precision_score(y_train_5, y_train_knn_pred)\n",
    "recall = recall_score(y_train_5, y_train_knn_pred)\n",
    "print('precision:{:.2f}%'.format(precision*100))\n",
    "print('recall:{:.2f}%'.format(recall*100))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.4"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
